---@class GT.selectUi 选择UI
---@field pb GT.ui.control.button[] 创建的技能按钮列表
---@field select_ui table<RoleID,GT.ui.model.selectUi[]> 创建的技能按钮列表
---@field OK UI_EVENT 确认
GT.selectUi = class()



---@param p GT.player
function GT.selectUi:ctor()
    self.pb = {}
    self.ui = {}
    --被选中的UI
    self.select_ui = OrderedTable()
    self.prefab = {}

    self.prefab["选择界面背景"] = GT.ui.prefab.obj({
        module = "022a44c8-89b3-4555-a869-86c007192889",
        parent = "panel_2",
    })
    local p = self.prefab["选择界面背景"]
    self.ui["self"] = p:child(GT.ui.control.image, "选择界面背景")
    self.ui["self"].binding.z_order = 999
    self.ui["选择列表"] = p:child(GT.ui.control.image, "选择列表")
    self.ui["选中列表"] = p:child(GT.ui.control.image, "选择列表.选中列表")
    self.ui["确定"] = p:child(GT.ui.control.button, "确定")
    self.ui["确定"]:click(function(data, player, ui)
        self:hide(player)
        GT.ui.model.sysMsgTip.obj({ prefix = '系统提示：', text = player:get_name() .. ' 已确认！' })
        if self.OK ~= nil then
            self.OK(data, player, ui)
        end
    end)
   self:tip()
end

function GT.selectUi:tip()
    local p = self.prefab["选择界面背景"]
    self.ui["选择提示"] =p:child(GT.ui, "选择提示")
    self.ui["name"] = p:child(GT.ui.control.label, "选择提示.name")
    self.ui["lv"] = p:child(GT.ui.control.label, "选择提示.lv")
    self.ui["icon"] = p:child(GT.ui.control.image, "选择提示.skill.icon")
    self.ui["cd"] = p:child(GT.ui.control.label, "选择提示.cd")

    self.ui["金币背景"] = p:child(GT.ui.control.image, "选择提示.金币")
    self.ui["人口背景"] = p:child(GT.ui.control.image, "选择提示.人口")

    self.ui["金币"] = p:child(GT.ui.control.label, "选择提示.金币.cost")
    self.ui["人口"] = p:child(GT.ui.control.label, "选择提示.人口.cost")

    self.ui["text"] = p:child(GT.ui.control.label, "选择提示.des.text")
    self.ui["des"] = p:child(GT.ui.control.scroll_view, "选择提示.des")
end

---设置tip信息
---@param p GT.player
---@param data buildAbilityTip_data
function GT.selectUi:set(p, data)
    self.ui["text"].u[p.pid].binding.text = data.text
    self.ui["name"].u[p.pid].binding.text = data.name
    self.ui["lv"].u[p.pid].binding.text = "Lv." .. data.lv
    self.ui["icon"].u[p.pid].binding.img = data.icon

    self.ui["cd"].u[p.pid].binding.text = ""

    local gold = data.gold or 0
    if gold > 0 then
        self.ui["金币背景"].u[p.pid].binding.show = true
        self.ui["金币"].u[p.pid].binding.text = data.gold or 0
    else
        self.ui["金币背景"].u[p.pid].binding.show = false
    end

    local pop = data.pop or 0

    if pop > 0 then
        self.ui["人口背景"].u[p.pid].binding.show = true
        self.ui["人口"].u[p.pid].binding.text = data.pop or 0
    else
        self.ui["人口背景"].u[p.pid].binding.show = false
    end
    self:tip_show(p)
end

function GT.selectUi:tip_show(p)
    self.ui["选择提示"].u[p.pid].binding.show = true
end

function GT.selectUi:tip_hide(p)
    self.ui["选择提示"].u[p.pid].binding.show = false
end

---设置确认按钮
---@param ok UI_EVENT
function GT.selectUi:confirm(ok)
    self.OK = ok
end

---创建技能按钮
---@param ability_build_Ids any
---@param p GT.player
function GT.selectUi:create_ski_btn(ability_build_Ids, p)
    local count = 0
    local row = 0
    for k, v in pairs(ability_build_Ids) do
        local pb = GT.ability.toBtn({
            key = v,
            x = 100 + (100 * count),
            y = 560 - (100 * row),
            show = false,
            parent = self.ui["选择列表"],
            click = function(data, player, ui)
                ---@type GT.ui.model.selectUi
                local u = ui
                u['key'] = v

                local pu = u.ui["self"].u[player.pid]
                local pu_bind = pu.binding
                if self.select_ui[player.pid] == nil then
                    self.select_ui[player.pid] = {}
                end
                for k, v in pairs(self.select_ui[player.pid]) do
                    if v.id == u.id then
                        local t1 = tween.new(0.5, pu_bind, { x = u.ui["self"].x, y = u.ui["self"].y }, 'linear')
                        tick.add(function(uuid)
                            if t1:update(0.02) then
                                tick.del(uuid)
                            end
                        end)
                        table.remove(self.select_ui[player.pid], k)
                        for k2, v2 in pairs(self.select_ui[player.pid]) do
                            if v2.ui["self"].u[player.pid].x ~= 46.0 + ((k2 - 1) * 100) then
                                local t2 = tween.new(0.5, v2.ui["self"].u[player.pid].binding,
                                    { x = 46.0 + ((k2 - 1) * 100), y = -90.62 },
                                    'linear')
                                tick.add(function(uuid)
                                    if t2:update(0.02) then
                                        tick.del(uuid)
                                    end
                                end)
                            end
                        end
                        return
                    end
                end



                pu_bind.y = u.ui["self"].y
                pu_bind.x = u.ui["self"].x

                table.insert(self.select_ui[player.pid], u)
                local count = TableLib.size(self.select_ui[player.pid])
                local t1 = tween.new(0.5, pu_bind, { x = 46.0 + ((count - 1) * 100), y = -90.62 }, 'linear')
                tick.add(function(uuid)
                    if t1:update(0.02) then
                        tick.del(uuid)
                    end
                end)
                -- for key, value in pairs(self.select_ui[player.pid]) do
                --     player:print_debug(value.data['key'])
                -- end
            end,
            enter_eve = function(data, player, ui)
                ---@type GT.ui.model.selectUi
                local u = ui
                local gold = 0
                local pop = 0
                if abilityInit[v] ~= nil then
                    gold = abilityInit[v].playerKv[Game.res_type['金币']] or 0
                    pop = abilityInit[v].playerKv[Game.res_type['人口']] or 0
                end

                local sb = StringBuilder.new()
                if abilityInit[v] ~= nil then
                    local unitdata = unitInit[abilityInit[v].build_unit_ID]
                    if unitdata ~= nil then
                        sb:append_line(GT.tip.attr(unitdata.attr))
                        for index, value in ipairs(unitdata.skills) do
                            sb:append_line('初始技能：' .. GT.ability.get_ability_name_by_key(value))
                            sb:append_line(GT.abilityInit.event_tip(value))
                        end
                        for key, value in pairs(unitdata.build_lvup) do
                            sb:append_line('等级：' .. key)
                            sb:append_line(GT.tip.attr(value.attr))
                            if value.skills ~= nil then
                                for ii, vv in ipairs(value.skills) do
                                    sb:append_line('技能：' .. GT.ability.get_ability_name_by_key(vv))
                                    sb:append_line(GT.abilityInit.event_tip(vv))
                                end
                            end
                        end
                    end
                end



                sb:append_line('详细：\n'..GT.ability.desc_key(v))

                self:set(player, {
                    icon = GT.ability.icon_id_key(v),
                    name = GT.ability.get_ability_name_by_key(v),
                    text = sb:tostring(true),
                    lv = 0,
                    cd = 0,
                    gold = gold,
                    pop = pop,
                })
                -- ---@type GT.ui.model.selectUi
                -- local u = ui
                -- u:set(player, {
                --     icon = GT.ability.icon_id_key(v),
                --     name = GT.ability.get_ability_name_by_key(v),
                --     text = sb:tostring(true),
                --     lv = 0,
                --     cd = 0,
                --     gold = gold,
                --     pop = pop,
                -- })
            end,
            leave_eve = function(data, player, ui)
                self:tip_hide(p)
            end
        })
        pb.ui['self'].u[p.pid].binding.show = true
        table.insert(self.pb, pb)
        count = count + 1
        if count >= (self.ui["选择列表"]:get_w() - 200) / 100 then
            row = row + 1
            count = 0
        end
    end
end

---显示
---@param p GT.player
function GT.selectUi:show(p)
    self.ui["self"].u[p.pid].binding.show = true
end

---隐藏
---@param p GT.player
function GT.selectUi:hide(p)
    self.ui["self"].u[p.pid].binding.show = false
end
